#include "treepictool.h"

TreePicTool::TreePicTool(QWidget *parent): QTreeWidget(parent)
{

}

void TreePicTool::mousePressEvent(QMouseEvent *event)
{
    if(event->button() & Qt::LeftButton)
    {
        emit itemClicked(itemFromIndex(indexAt(event->pos())),indexAt(event->pos()).column());
    }
    m_pressPoint = event->pos();
    m_isMoving = false;

    m_taskName = itemAt(event->pos())->text(0);     // 获取任务名称
    QTreeWidget::mousePressEvent(event);
}

void TreePicTool::mouseMoveEvent(QMouseEvent *event)
{
    if(m_isMoving || QLineF(event->screenPos(), m_pressPoint).length() < QApplication::startDragDistance())
    {
        return ;  // 小于默认拖动距离(10个像素点)就退出
    }
    m_isMoving = true;
    QDrag *drag = new QDrag(this);
    QMimeData *mimeData = new QMimeData();
    drag->setMimeData(mimeData);                    // 设置数据传输载体

    QByteArray byteArray(m_taskName.toUtf8());      // 加密任务数据
    mimeData->setData("transfer", byteArray);       // 发送加密数据，加密标记为“transfer”

    drag->exec();       // 开启拖动事件
}
